﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 暑假作业
{
    public partial class goodsInfo_update : Form
    {
        public goodsInfo_update()
        {
            InitializeComponent();
        }
        public goodsInfo goodsInfo;
        //行索引
        public int id;
        /*//各列变量
        public int goodsid;
        public string goodsname;
        public string UnitPrice;
        public string Manufactrue;
        public string category;*/
        private void goodsInfo_update_Load(object sender, EventArgs e)
        {
            int rowindex=id;
            //焦点和加载时
            textBox2.Focus();
            //选中数据展示
            textBox1.Text = goodsInfo.dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
            textBox2.Text = goodsInfo.dataGridView1.Rows[rowindex].Cells[1].Value.ToString();
            textBox3.Text = goodsInfo.dataGridView1.Rows[rowindex].Cells[2].Value.ToString();
            textBox4.Text = goodsInfo.dataGridView1.Rows[rowindex].Cells[3].Value.ToString();
            comboBox1.Text = goodsInfo.dataGridView1.Rows[rowindex].Cells[4].Value.ToString();
        }
        //提交
        private void button1_Click(object sender, EventArgs e)
        {
            //非空验证
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
            {
                var sql = $"update goods set gname='{textBox2.Text}',unitprice='{textBox3.Text}',Manufactrue='{textBox4.Text}',category='{comboBox1.Text}' " +
                    $"where goodsid='{textBox1.Text}'";
                var result = DBHelper.insert(sql);
                if (result==1)
                {
                    MessageBox.Show("更新成功");
                    var sql1 = "select  goodsid,GName,UnitPrice,Manufactrue,category from goods";
                    DataSet ds = DBHelper.show(sql1);
                    goodsInfo.dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("更新失败");
                }
            }
            else
            {
                MessageBox.Show("请输入信息");
            }
        }
        //关闭
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
